package com.caimore.module.ec200n;

import com.caimore.common.Serial_For_Smslib;
import com.caimore.common.SmsUtils_sdk;
import com.caimore.entity.SMSMessageMgr;
import com.caimore.entity.SendSmsParam;
import com.caimore.modulefactory.IsmsWriter;
import java.util.ArrayList;

public class ATWriterThreadPduEC200N implements IsmsWriter {

   Serial_For_Smslib SerilaSmslib;
   private volatile boolean shutdown = false;
   private volatile boolean sendMs = false;
   static final int PDUlen = 64;
   static final int i7bitLen = 134;
   Thread t1 = null;


   public ATWriterThreadPduEC200N(Serial_For_Smslib _smslib_test, boolean _shutdown) {
      this.SerilaSmslib = _smslib_test;
      this.shutdown = _shutdown;
   }

   public void start() {
      this.t1 = new Thread() {
         public void run() {
            String e;
            try {
               e = "AT+CPIN ? \r";
               char[] sReceivedNum = e.toCharArray();
               ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(sReceivedNum);
               System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + e);

               while(!SMSMessageMgr.getInstance().getREADY() && !SMSMessageMgr.getInstance().getREADY()) {
                  ;
               }

               if(SMSMessageMgr.getInstance().getREADY()) {
                  for(int oSendSmsParam = 0; oSendSmsParam < 4; ++oSendSmsParam) {
                     String j;
                     char[] s_CMGL;
                     if(oSendSmsParam == 0) {
                        j = "at+csq\r";
                        s_CMGL = j.toCharArray();
                        ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(s_CMGL);
                        System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + j);
                        Thread.sleep(500L);
                     } else if(oSendSmsParam == 1) {
                        j = "at+creg? \r";
                        s_CMGL = j.toCharArray();
                        ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(s_CMGL);
                        System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + j);
                        Thread.sleep(500L);
                     } else if(oSendSmsParam == 2) {
                        j = "AT+CSCA ? \r";
                        s_CMGL = j.toCharArray();
                        ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(s_CMGL);
                        System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + j);
                        Thread.sleep(500L);
                     } else if(oSendSmsParam == 3) {
                        j = "AT+CMGF=0\r";
                        s_CMGL = j.toCharArray();
                        ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(s_CMGL);
                        System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + j);
                        Thread.sleep(2000L);
                     }
                  }
               }
            } catch (Exception var21) {
               System.out.println(var21.getMessage());
            }

            while(!ATWriterThreadPduEC200N.this.shutdown) {
               try {
                  if(SMSMessageMgr.getInstance().getREADY()) {
                     e = null;
                     String var22 = null;
                     SendSmsParam var23 = SMSMessageMgr.getInstance().delSendSmsParam();
                     String var26;
                     if(var23 != null) {
                        e = var23.getContent();
                        var22 = var23.getPhone();
                        ArrayList var25 = new ArrayList();
                        if(e != null) {
                           if(e.length() <= 64) {
                              var25.add(e);
                           } else {
                              while(e.length() > 64) {
                                 var26 = e.substring(0, 64);
                                 var25.add(var26);
                                 e = e.substring(64);
                                 if(e.length() <= 64) {
                                    var25.add(e);
                                 }
                              }
                           }

                           int var28 = 0;

                           while(var28 < var25.size()) {
                              String var27 = (String)var25.get(var28);
                              String sSeriNo = var28 + 1 < 9?"0" + (var28 + 1):"" + (var28 + 1);
                              String sAllNo = var25.size() < 9?"0" + var25.size():"" + var25.size();
                              System.out.println("[ATReaderThreadPduEC200N]sAllNo: " + sAllNo + " , sSeriNo: " + sSeriNo);
                              char[] chSendContext = null;

                              for(int j1 = 0; j1 < 3; ++j1) {
                                 System.out.println("SmsUtils.getInstance().getOK():" + SMSMessageMgr.getInstance().getOK());
                                 System.out.println("SmsUtils.getInstance().getCR():" + SMSMessageMgr.getInstance().getCR());
                                 String newOrder;
                                 if(j1 == 0) {
                                    newOrder = "AT+CMGF=0\r";
                                    char[] sContextHeader = newOrder.toCharArray();
                                    ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(sContextHeader);
                                    System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + newOrder);
                                    Thread.sleep(2000L);
                                 } else if(j1 == 1) {
                                    newOrder = SMSMessageMgr.getInstance().getCSCA();

                                    while(newOrder == null) {
                                       newOrder = SMSMessageMgr.getInstance().getCSCA();
                                       if(newOrder != null) {
                                          break;
                                       }
                                    }

                                    String var29 = "91" + SmsUtils_sdk.addFandSwap(newOrder);
                                    if(var29.length() / 2 >= 10) {
                                       var29 = var29.length() / 2 + var29;
                                    } else {
                                       var29 = "0" + var29.length() / 2 + var29;
                                    }

                                    String sSendContextBody = null;
                                    String sTempReceivedNum = SmsUtils_sdk.sendReceivedNum(var22);
                                    System.out.println(" [ATReaderThreadPduEC200N] SmsUtils.isUCS2(sTempBody) ");
                                    byte[] bySendContextPDU = SmsUtils_sdk.encodeUCS2_005100(var27);
                                    sSendContextBody = SmsUtils_sdk.byte2hex((byte)(bySendContextPDU.length + 6)) + "050003" + sAllNo + sAllNo + sSeriNo + SmsUtils_sdk.bytesToHexString(bySendContextPDU);
                                    sSendContextBody = sTempReceivedNum + "000800" + sSendContextBody;
                                    String sContextTemp = var29 + sSendContextBody;
                                    chSendContext = sContextTemp.toCharArray();
                                    String atCommand = "AT+CMGS=" + sSendContextBody.length() / 2 + "\r";
                                    char[] atOrder = atCommand.toCharArray();
                                    ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(atOrder);
                                    System.out.println("[ATReaderThreadPduEC200N]sSend-ContextHeader:" + atCommand);
                                    Thread.sleep(2000L);
                                 } else if(j1 == 2) {
                                    if(chSendContext != null) {
                                       char[] var30 = new char[chSendContext.length + 1];
                                       System.arraycopy(chSendContext, 0, var30, 0, chSendContext.length);
                                       var30[chSendContext.length] = 26;
                                       ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(var30);
                                       System.out.println("[ATReaderThreadPduEC200N]sSend-ContextBody:" + new String(chSendContext));
                                       System.out.println("\r\n\r\n");
                                       Thread.sleep(2000L);
                                    }

                                    ++var28;
                                 }
                              }

                              Thread.sleep(2000L);
                           }

                           ATWriterThreadPduEC200N.this.sendMs = false;
                        }
                     } else {
                        for(int var24 = 0; var24 < 4; ++var24) {
                           System.out.println("SmsUtils.getInstance().getOK():" + SMSMessageMgr.getInstance().getOK());
                           System.out.println("SmsUtils.getInstance().getCR():" + SMSMessageMgr.getInstance().getCR());
                           char[] atCMGL;
                           if(var24 == 0) {
                              var26 = "AT+CMGR=4\r";
                              atCMGL = var26.toCharArray();
                              ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(atCMGL);
                              System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + var26);
                              Thread.sleep(2000L);
                           }

                           if(var24 == 1) {
                              var26 = "AT+CMGL=4\r";
                              atCMGL = var26.toCharArray();
                              ATWriterThreadPduEC200N.this.SerilaSmslib.writeByte(atCMGL);
                              System.out.println("[ATReaderThreadPduEC200N]sSend-AT-Command:" + var26);
                              Thread.sleep(2000L);
                           }
                        }
                     }
                  }

                  Thread.sleep(2000L);
               } catch (InterruptedException var19) {
                  Thread.currentThread().interrupt();
               } catch (Exception var20) {
                  System.out.println(var20.getMessage());
               }
            }

         }
      };
      this.t1.start();
   }

   public final void shutdownRequest() {
      this.shutdown = true;
      this.t1.interrupt();
   }
}
